<script>
  function replace_string(find_string,replace_string)
  {
    var temp_string=this;
    var left_string;
    var right_string;
    var start_location;

    var replace_location=temp_string.indexOf(find_string);
    while(replace_location != -1)
    {
      left_string=temp_string.left(replace_location);
      right_string=temp_string.substring(replace_location+find_string.length);
      temp_string=left_string + replace_string + right_string;

      start_location=replace_location + replace_string.length;
      replace_location=temp_string.indexOf(find_string,start_location);
      //replace_location=temp_string.indexOf(find_string);
    }
    return temp_string;
  }

  function extract_left(total_chars)
  {
    return this.substring(0,total_chars);
  }

  String.prototype.left=extract_left;
  String.prototype.replace=replace_string;

  var my_string ="HAVEAAB";
  var replaced_string=my_string.replace("AB","BC");

  with(document)
  {
    write("<b>Original String: </b><br>");
    write(my_string);
    write("<p><b>Result of my_string </b><br>");
    write(replaced_string);
  }
</script>
